layui.define(['element', 'exception', 'common'], function (exports) {
    "use strict";
    var $ = layui.jquery,
        element = layui.element,
        exception = layui.exception,
        common = layui.common,
        cacheName = 'gtmap-slidetab';
    var SlideTab = function () {
        this.info = {
            name: 'SlideTab',
            version: 'v1.1',
            el: '',//默认容器
            data: 'main/loadSideBarNodes'
        };
        this.config = {
            el: undefined,
            data: undefined
        };
    };
    //渲染
    SlideTab.prototype.render = function (options,callback) {
        var _that = this;
        var _config = _that.config;
        if (options === undefined || options === null) {
            //如果未配置
            _config.el = _that.info.el;
            _config.data = _that.info.data;
        } else {
            _that.config.data = undefined;
            $.extend(true, _that.config, options);
        }
        var $container = common.getContainer(_that);
        if ($container === null) {
            return;
        }
        //检查是否存在缓存
        var slideTabData = null;
        var cache = layui.data(cacheName);
        if (cache !== undefined && cache !== null && cache.value !== undefined && cache.value !== null) {
            slideTabData = cache;
        } else if (_config.data !== undefined && _config.data !== null) {
            $.ajax({
                type: 'get',
                url: _config.data,
                async: false,
                dataType: 'json',
                success: function (res, status, xhr) {
                    console.log(res);
                    if (res.code == 0) {
                        slideTabData = res.data;

                    }
                },
                error: function (xhr, status, error) {
                    exception.throwError("slidetab Error:" + error);
                },
                complete: function (xhr, status) {
                    _that.config.el = $container;
                }
            });
        } else {
//          exception.throwDataCinfigError(_that, 'data');
        }
        //初始化必须的元素节点
        console.log($container)
        initHtml($container);
        slide($container);
    };

    /**
     * 清除缓存
     */
    SlideTab.prototype.cleanCached = function () {
        layui.data(cacheName, null);
    };
    //初始化页面代码，方便让后边的动态渲染
    function initHtml(container){
        var preNextBtn = '<a href="javascript:;" class="gt-prev"><span></span></a>' + '<a href="javascript:;" class="gt-next"><span></span></a>';
        $(container).find('.layui-tab').prepend(preNextBtn);
    }
    // 拼接

    function slide(container){
        //按钮箭头
        var catew = $('.gt-tab-slide').width()-120;
        var cateLiw = 0;
        $('.gt-tab-slide ul li').each(function(){
            cateLiw +=$(this).outerWidth();
        })
        var i = 0;
        $(container).on('click','.gt-next',function(){
            $('.gt-tab-slide ul').animate({
                "margin-left": - catew * (i + 1) + 28 + 'px'
            },500)
            i++;
            if(catew * (i + 1) >= cateLiw){
                $('.gt-prev').show();
                $('.gt-next').hide();
                i = 0;
            }
        })
        var j = Math.floor(cateLiw/(catew+120));
        $(container).on('click','.gt-prev',function(){
            $('.gt-tab-slide ul').animate({
                "margin-left":-catew * (j-1) + 'px'
            },500)
            j--;
            if(j == 0){
                $(this).hide();
                $('.gt-next').show();
                j = Math.floor(cateLiw/(catew+120));
            }

        })

        var tabsWidth = 0;
        $('.layui-tab-title-more li').each(function () {
            console.log($(this).width())
            tabsWidth += $(this).width()+32;
        })
        if(tabsWidth < $('.layui-tab-title-more').width()){
            $('.gt-next').css('display','none')
        }else{
            $('.gt-next').css('display','block')
        }
    }
    // 导出
    var slidetab = new SlideTab();
    exports('tab', slidetab);
});
